const mongoose = require("./index");
const Schema = mongoose.Schema;

/**
 * 用户表
 * name:用户名
 * phone：手机号（用于登录）
 * password：密码
 * sex：性别
 * img:头像
 * face:面部
 * state：账号状态
 * roleId:该用户的角色
 * companyId:该用户所属公司
 * articleId:该用户发布的文章
 * like：该用户收藏的文章
 */
const userSchema = new Schema({
  name: String,
  phone: String,
  password: String,
  sex: String,
  img: String,
  face: String,
  state: { type: Boolean, default: true },
  roleId: {
    type: mongoose.Types.ObjectId,
    ref: "role",
  },
  companyId: {
    type: mongoose.Types.ObjectId,
    ref: "company",
  },
  articleId: [
    {
      type: mongoose.Types.ObjectId,
      ref: "article",
    },
  ],
  like: [
    {
      type: mongoose.Types.ObjectId,
      ref: "article",
    },
  ],
});
/**
 * 角色表
 * name:角色名称
 * rightId:权限
 *
 */
const roleSchema = new Schema({
  name: String,
  rightId: [
    {
      type: mongoose.Types.ObjectId,
      ref: "right",
    },
  ],
});
/**
 * 权限
 * name:权限名称
 * path:路由权限路径
 * icon：图标
 * type：路由/按钮权限
 * pid:父级ID
 */
const rightSchema = new Schema({
  name: String,
  path: String,
  icon: String,
  type: String,
  pid: {
    type: mongoose.Types.ObjectId,
    ref: "right",
  },
});
/**
 * 公司表
 * name:公司名称
 * desc：公司简介
 * city: 所在城市
 */
const companySchema = new Schema({
  name: String,
  desc: String,
  city: String,
});
/**
 * 文章表
 * title:标题
 * tags：文章标签
 * txt：内容
 * userId：用于获取作者信息
 * type:用于分类该文章
 * technologyTag：技术栈标签
 * cityTag：城市标签
 * status:文章的状态
 */
const articleSchema = new Schema({
  title: String,
  type: String,
  technologyTag: [String],
  cityTag: [String],
  companyTag: [String],
  txt: String,
  createDate:{type:Date,default:new Date()},
  status:String,
  userId: { type: mongoose.Types.ObjectId, ref: "user" },
});

/**
 * 评论表
 * interviewId：存储面试题Id
 * userId：评论者ID
 * pid:父ID
 */
const commentSchema = new Schema({
  articleId: { type: mongoose.Types.ObjectId, ref: "article" },
  userId: { type: mongoose.Types.ObjectId, ref: "user" },
  pid: { type: mongoose.Types.ObjectId, ref: "comment" },
});

const userModel = mongoose.model("user", userSchema, "user");
const roleModel = mongoose.model("role", roleSchema, "role");
const rightModel = mongoose.model("right", rightSchema, "right");
const companyModel = mongoose.model("company", companySchema, "company");
const articleModel = mongoose.model("article", articleSchema, "article");
const commentModel = mongoose.model("comment", commentSchema, "comment");

// userModel.create({
//   name: 'admin',
//   phone: '13273102959',
//   password: 'wyt0816',
//   sex: '男',
// })
module.exports = {
  userModel,
  roleModel,
  rightModel,
  companyModel,
  articleModel,
  commentModel,
};

